var pomelo = require('pomelo');
var utils = require('../../util/utils');
var logger = require('pomelo-logger').getLogger('KillerGame', __filename);
var Consts = require('../../util/consts');

var userTempInfoDao = module.exports;


// 新建用户
userTempInfoDao.createUser = function (account, nickname, cb) {
    var sql = 'replace into user_temp_info (account, nickname) values (?, ?)';
    var args = [account, nickname];
    pomelo.app.get('dbclient').query(sql, args, function (err, res) {
        if (!!err) {
            utils.invokeCallback(cb, {code: err.number, msg: err.message}, null);
        } else {
            var user = {account: account, nickname: nickname};
            utils.invokeCallback(cb, null, user);
        }
    });
};

// 通过account获取用户全部信息
userTempInfoDao.getUserInfo = function (account, cb) {
    var sql = 'select * from user_temp_info where account = ? ';
    var args = [account];
    pomelo.app.get('dbclient').query(sql, args, function (err, res) {
        if (!!err) {
            utils.invokeCallback(cb, {code: err.number, msg: err.message}, null);
        } else {
            if (res && res.length > 0) {
                var user = res[0];
                utils.invokeCallback(cb, null, user);
            } else {
                utils.invokeCallback(cb, null, {});
            }

        }
    });
};

// 通过account list获取用户全部信息
userTempInfoDao.getUserInfoList = function (accountList, cb) {
    var sql = 'select * from user_temp_info where account in (?)';
    var args = [accountList];
    pomelo.app.get('dbclient').query(sql, args, function (err, res) {
            if (!!err) {
                utils.invokeCallback(cb, {code: err.number, msg: err.messsage}, null);
            } else {
                var userList = [];
                if (res && res.length > 0) {
                    for (var i = 0; i < res.length; i++) {
                        var dbUser = res[i];
                        userList.push({
                            account: dbUser.account,
                            nickname: dbUser.nickname
                        });
                    }
                    utils.invokeCallback(cb, null, userList);
                } else {
                    utils.invokeCallback(cb, null, []);
                }
            }
        }
    );
};